package com.dragon.manage.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.DuibaExchange;
import com.dragon.manage.bean.Recharge;
import com.dragon.manage.bean.UserBet;
import com.dragon.manage.bean.UserInfo;
import com.dragon.manage.dao.IAmusementInfoDAO;
import com.dragon.manage.dao.IAmusementTypeDAO;
import com.dragon.manage.dao.ICacheDao;
import com.dragon.manage.dao.IDuibaExchangeDAO;
import com.dragon.manage.dao.IMatchInfoDAO;
import com.dragon.manage.dao.IRechargeDAO;
import com.dragon.manage.dao.ITeamInfoDAO;
import com.dragon.manage.dao.IUserBetDAO;
import com.dragon.manage.dao.IUserInfoDAO;
import com.dragon.manage.service.IManageUserInfoService;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.TimeUtil;


public class ManageUserInfoServiceImpl implements IManageUserInfoService{

	/** 用户信息 */
	private IUserInfoDAO userInfoDAO;
	/** 充值信息 */
	private IRechargeDAO rechargeDAO;
	/** 用户下注信息 */
	private IUserBetDAO userBetDAO;
	/**缓存信息 */
	private ICacheDao cacheDAO;
	
	private IDuibaExchangeDAO duibaExchageDAO;
	
	/** 竞猜信息 */
	private IAmusementInfoDAO amusementInfoDAO;
	/** 战队信息*/
	private ITeamInfoDAO teamInfoDAO;
	/** 比赛信息**/
	private IMatchInfoDAO matchInfoDAO;
	/**
	 * 开盘类型
	 */
	private IAmusementTypeDAO amusementTypeDAO;
	
	public void setAmusementInfoDAO(IAmusementInfoDAO amusementInfoDAO) {
		this.amusementInfoDAO = amusementInfoDAO;
	}
	
	public void setTeamInfoDAO(ITeamInfoDAO teamInfoDAO) {
		this.teamInfoDAO = teamInfoDAO;
	}

	public void setMatchInfoDAO(IMatchInfoDAO matchInfoDAO) {
		this.matchInfoDAO = matchInfoDAO;
	}

	public void setAmusementTypeDAO(IAmusementTypeDAO amusementTypeDAO) {
		this.amusementTypeDAO = amusementTypeDAO;
	}

	public void setDuibaExchageDAO(IDuibaExchangeDAO duibaExchageDAO) {
		this.duibaExchageDAO = duibaExchageDAO;
	}
	
	public ICacheDao getCacheDAO() {
		return cacheDAO;
	}

	public void setCacheDAO(ICacheDao cacheDAO) {
		this.cacheDAO = cacheDAO;
	}

	public IUserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(IUserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	public IRechargeDAO getRechargeDAO() {
		return rechargeDAO;
	}

	public void setRechargeDAO(IRechargeDAO rechargeDAO) {
		this.rechargeDAO = rechargeDAO;
	}

	public IUserBetDAO getUserBetDAO() {
		return userBetDAO;
	}

	public void setUserBetDAO(IUserBetDAO userBetDAO) {
		this.userBetDAO = userBetDAO;
	}

	@Override
	public JSONObject getUserInfoList(int type,int page) throws Exception {
		JSONObject reJs = new JSONObject();
		JSONArray reArray = new JSONArray();
		JSONObject jsTmp = null;
		List<UserInfo> userInfoList = userInfoDAO.getUserInfoList(type, page);
		for(UserInfo tmp:userInfoList){
			jsTmp = ManageUtil.getWhiteUserPage(tmp);
			reArray.add(jsTmp);
		}
		reJs.put("userList", reArray);
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), userInfoDAO.getUserInfoCount(type));
		}
		return reJs;
	}

	@Override
	public JSONObject checkRecharge(int userId, int page,int type) throws Exception {
		JSONObject reJs = new JSONObject();
		JSONArray reArray = new JSONArray();
		JSONObject jsTmp = null;
		List<Recharge> rechargeList = rechargeDAO.getRechargeList(userId,
				page, StaticData.no_param);
		for(Recharge tmp:rechargeList){
			jsTmp = ManageUtil.getUserRechargeListPage(tmp);
			reArray.add(jsTmp);
		}
		reJs.put("rechargeList", reArray);
		reJs.put("rechargeSum", rechargeDAO.getRechargeSum(userId, 
				StaticData.rechargeOrderStatus.used.getType(),type));
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), rechargeDAO.getRechargeCount(userId,
					StaticData.no_param));
		}
		return reJs;
	}

	@Override
	public JSONObject userProfitTotal(int userId, int type) throws Exception {
		JSONObject reJs = new JSONObject();
		UserInfo userInfo = userInfoDAO.getUserInfoByID(userId);
		if(userInfo==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.user_not_exist.getType());
			return reJs;
		}
		int total = cacheDAO.getUserProfitTotal(userId, null, null, type);
		reJs.put(StaticData.reKey.sum.getKey(),total);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJs;
	}

	@Override
	public JSONObject userBetDetail(int userId, int page)throws Exception {
		List<UserBet> list = userBetDAO.getUserBetList(userId, page);
		JSONObject reJs = new JSONObject();
		JSONArray reArray = new JSONArray();
		for(UserBet tmp:list){
//			reArray.add(ManageUtil.getUserBetListPage(tmp));
			reArray.add(ManageUtil.getMatchInfoPage(tmp, amusementInfoDAO, 
					matchInfoDAO, teamInfoDAO, amusementTypeDAO));
		}
		reJs.put("userBetList", reArray);
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), userBetDAO.getUserBetCount(userId, StaticData.no_param));
		}
		return reJs;
	}

	@Override
	public JSONObject userExchangeList(int userId, int page) throws Exception {
		JSONObject reJs = new JSONObject();
		JSONArray reArray = new JSONArray();
		List<DuibaExchange> list = duibaExchageDAO.getDuibaExchangeList(userId, page);
		if(list!=null){
			JSONObject reTmp = null;
			for(DuibaExchange tmp:list){
				reTmp = new JSONObject();
				reTmp.put("id", tmp.getId());
				reTmp.put("desc", tmp.getDescription());
				reTmp.put("ordernum", tmp.getOrdernum());
				reTmp.put("price", tmp.getFaceprice());
				reTmp.put("status", tmp.getDStatus());
				reTmp.put("createDate", TimeUtil.getDateFormat(tmp.getCreateDate()));
				reArray.add(reTmp);
			}
		}
		reJs.put("reList", reArray);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), duibaExchageDAO.getDuibaExchageCount(userId));
		}
		return reJs;
	}
	
	

}
